
package ru.kpm.rectpack.represent;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import ru.kpm.rectpack.utils.RectPacking;

/**
 *
 * @author Podorov Alexander
 */

public class OutputData
{
    private List<Rectangle> pallets;
    private List<List<Rectangle>> packs;
    private int height1;
    private int height2;
    private int palletsInContainer;
    private int palletsInLevel;
    private int packsInContainer;
    private int packsIn1LevelPallet;
    private int packsIn2LevelPallet;
    private int packsIn1Level;
    private int packsIn2Level;
    private int palletsHeight;
    private long containerVolume;
    private long effectiveContainerVolume;
    private double efficiency;

    public OutputData()
    {        
        packs = new ArrayList<List<Rectangle>>();
    }

    public void processData(InputData data)
    {        
        packs.clear();

        pallets = RectPacking.optimize(data.getCx(), data.getCy(), data.getDcx(), data.getDcy(), data.getPx(), data.getPy());
        ArrayList<Rectangle> horizontal = null;
        for (Rectangle rectangle : pallets)
        {
            if (rectangle.getWidth() >= rectangle.getHeight())
            {
                horizontal = (ArrayList<Rectangle>)RectPacking.optimize((int)rectangle.getWidth(), (int)rectangle.getHeight(), data.getDpx(), data.getDpy(), data.getX(), data.getY());
                break;
            }
        }
        ArrayList<Rectangle> vertical = null;
        for (Rectangle rectangle : pallets)
        {
            if (rectangle.getWidth() < rectangle.getHeight())
            {
                vertical = (ArrayList<Rectangle>)RectPacking.optimize((int)rectangle.getWidth(), (int)rectangle.getHeight(), data.getDpx(), data.getDpy(), data.getX(), data.getY());
                break;
            }
        }
        for (Rectangle rectangle : pallets)
        {
            if (rectangle.getWidth() >= rectangle.getHeight())
            {
                packs.add((ArrayList<Rectangle>)horizontal.clone());
            }
            else
            {
                packs.add((ArrayList<Rectangle>)vertical.clone());
            }
        }

        height1 = data.getLayers1() * data.getZ();
        height2 = data.getLayers2() * data.getZ();

        palletsHeight = 4 * data.getDpz() + height1 + height2;

        int packsInPallet = 0;
        if (packs.size() > 0)
        {
            packsInPallet = packs.get(0).size();
        }
        else
        {
            packsInPallet = 0;
        }

        palletsInContainer = data.getPLayers() * pallets.size();
        palletsInLevel = pallets.size();
        packsInContainer = pallets.size() * packsInPallet * (data.getLayers1() + data.getLayers2());
        packsIn1LevelPallet = packsInPallet * data.getLayers1();
        packsIn2LevelPallet = packsInPallet * data.getLayers2();
        packsIn1Level = packsIn1LevelPallet * pallets.size();
        packsIn2Level = packsIn2LevelPallet * pallets.size();
        containerVolume = (long)data.getCx() * (long)data.getCy() * (long)data.getCz();
        effectiveContainerVolume = 0;
        for (Rectangle rectangle : pallets)
        {
            effectiveContainerVolume += (long)(rectangle.getWidth() * rectangle.getHeight());
        }
        effectiveContainerVolume *= height1 + height2;
        efficiency = (effectiveContainerVolume * 100.0) / containerVolume;
    }

    public int getPalletsInLevel()
    {
        return palletsInLevel;
    }

    public int getPalletsHeight()
    {
        return palletsHeight;
    }

    public long getContainerVolume()
    {
        return containerVolume;
    }

    public long getEffectiveContainerVolume()
    {
        return effectiveContainerVolume;
    }

    public double getEfficiency()
    {
        return efficiency;
    }

    public List<List<Rectangle>> getPacks()
    {
        return packs;
    }

    public int getPacksIn1Level()
    {
        return packsIn1Level;
    }

    public int getPacksIn1LevelPallet()
    {
        return packsIn1LevelPallet;
    }

    public int getPacksIn2Level()
    {
        return packsIn2Level;
    }

    public int getPacksIn2LevelPallet()
    {
        return packsIn2LevelPallet;
    }

    public int getPacksInContainer()
    {
        return packsInContainer;
    }

    public List<Rectangle> getPallets()
    {
        return pallets;
    }

    public int getPalletsInContainer()
    {
        return palletsInContainer;
    }

    public int getHeight1()
    {
        return height1;
    }

    public int getHeight2()
    {
        return height2;
    }
}
